package com.google.code.irouter.portal.client.dashboard.main.center.managers.queue;

import java.util.ArrayList;
import java.util.List;

import com.google.code.irouter.portal.client.AppConstants;
import com.google.code.irouter.portal.client.dashboard.main.center.managers.queue.browser.Command;
import com.google.code.irouter.portal.client.dashboard.main.north.menu.switcher.Solutions;
import com.google.code.irouter.portal.client.rpc.queue.QueueResult;
import com.google.code.irouter.portal.client.rpc.queue.QueueService;
import com.google.code.irouter.portal.client.rpc.queue.QueueServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.grid.ColumnConfig;
import com.gwtext.client.widgets.grid.ColumnModel;
import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.grid.event.GridRowListenerAdapter;

public final class Browser extends GridPanel {
	
	private static final AppConstants CONSTANTS = GWT.create(AppConstants.class);
	private final QueueServiceAsync queueService = GWT.create(QueueService.class);

	public Browser() {
		super();
		this.setId(Browser.class.getName());
		
		this.setTopToolbar(new Command());
		this.setBorder(false);
		ColumnConfig[] columns = {
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_queue_browser_column_message(),"message", 500, true),
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_queue_browser_column_link(),"link", 200, true),
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_queue_browser_column_fixed(),"fixedLink", 200, true),
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_queue_browser_column_solution(),"solution", 100, true),
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_queue_browser_column_relationship(),"relationship", 100, true),
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_queue_browser_column_consumer(),"consumer", 100, true),
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_queue_browser_column_create(),"createDate", 150, true),
				new ColumnConfig(CONSTANTS.com_google_code_irouter_portal_client_dashboard_main_center_managers_queue_browser_column_update(),"updateDate", 150, true)
		};
		ColumnModel columnModel = new ColumnModel(columns);
		this.setColumnModel(columnModel);
		Store store = new SimpleStore(new String[] { "message", "link", "fixedLink", "solution", "relationship", "consumer", "createDate", "updateDate", "key" }, new Object[0][0]);
		store.load();
		this.setStripeRows(true);
		this.setStore(store);
		this.show();
		this.addGridRowListener(new GridRowListenerAdapter() {
			@Override
			public void onRowDblClick(GridPanel grid, int rowIndex, EventObject e) {
				Window.open(getStore().getAt(rowIndex).getAsString("link"), "_blank", "");
			}
		});
	}
	
	public void reset() {
		Store store = new SimpleStore(new String[] { "message", "link", "fixedLink", "solution", "relationship", "consumer", "createDate", "updateDate", "key" }, new Object[0][0]);
		store.load();
		reconfigure(store, getColumnModel());
	}
	
	public void refresh() {
		getEl().mask(CONSTANTS.com_google_code_irouter_portal_client_common_processing(), true);
		String solutionId = "";
		String cookieId = Cookies.getCookie(Solutions.COOKIE_SOLUTION);
		if(cookieId != null)
			if(cookieId.trim().length() > 0)
				solutionId = cookieId;
		if(solutionId.length() == 0) {
			reset();
			getEl().unmask();
		}
		queueService.load(
				solutionId,
				new AsyncCallback<List<QueueResult>>() {
					public void onFailure(Throwable caught) {
					// TODO
						reset();
						getEl().unmask();
					}

					public void onSuccess(List<QueueResult> queueResultList) {
						if(queueResultList != null) {
							Object[][] queueArray = new Object[queueResultList.size()][9];
							int index = 0;
							for (QueueResult queueResult : queueResultList) {
								Object[] queueItem = new Object[9];
								queueItem[0] = queueResult.getMessage();
								queueItem[1] = queueResult.getLink();
								queueItem[2] = queueResult.getFixedLink();
								queueItem[3] = queueResult.getSolution();
								queueItem[4] = queueResult.getRelationship();
								queueItem[5] = queueResult.getConsumer();
								queueItem[6] = queueResult.getCreateDate();
								queueItem[7] = queueResult.getUpdateDate();
								queueItem[8] = String.valueOf(queueResult.getId());
								queueArray[index] = queueItem;
								index++;
							}
							Store store = new SimpleStore(new String[] { "message", "link", "fixedLink", "solution", "relationship", "consumer", "createDate", "updateDate", "key" }, queueArray);
							store.load();
							reconfigure(store, getColumnModel());
						} else {
							reset();
						}
						getEl().unmask();
					}
				}
		);
	}

	public void remove() {
		getEl().mask(CONSTANTS.com_google_code_irouter_portal_client_common_processing(), true);
		Record[] records = getSelectionModel().getSelections();
		if(records.length > 0) {
			final List<String> idList = new ArrayList<String>();
			for(Record record : records) {
				idList.add(record.getAsString("key"));				
			}		
			queueService.remove(
					idList.toArray(new String[0]), 
					new AsyncCallback<Boolean>() {
						public void onFailure(Throwable caught) {
						// TODO
							getEl().unmask();
						}
		
						public void onSuccess(Boolean result) {
							if(result) {
								refresh();
							}
							getEl().unmask();
						}
					}
				);
		} else {
			getEl().unmask();
		}
	}
}
